{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# ski_assignment"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/ski_assignment.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/ski_assignment.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  Ski assignment in Google CP Solver.\n",
    "\n",
    "  From   Jeffrey Lee Hellrung, Jr.:\n",
    "  PIC 60, Fall 2008 Final Review, December 12, 2008\n",
    "  http://www.math.ucla.edu/~jhellrun/course_files/Fall%25202008/PIC%252060%2520-%2520Data%2520Structures%2520and%2520Algorithms/final_review.pdf\n",
    "  '''\n",
    "  5. Ski Optimization! Your job at Snapple is pleasant but in the winter\n",
    "  you've decided to become a ski bum. You've hooked up with the Mount\n",
    "  Baldy Ski Resort. They'll let you ski all winter for free in exchange\n",
    "  for helping their ski rental shop with an algorithm to assign skis to\n",
    "  skiers. Ideally, each skier should obtain a pair of skis whose height\n",
    "  matches his or her own height exactly. Unfortunately, this is generally\n",
    "  not possible. We define the disparity between a skier and his or her\n",
    "  skis to be the absolute value of the difference between the height of\n",
    "  the skier and the pair of skis. Our objective is to find an assignment\n",
    "  of skis to skiers that minimizes the sum of the disparities.\n",
    "  ...\n",
    "  Illustrate your algorithm by explicitly filling out the A[i, j] table\n",
    "  for the following sample data:\n",
    "    * Ski heights: 1, 2, 5, 7, 13, 21.\n",
    "    * Skier heights: 3, 4, 7, 11, 18.\n",
    "  '''\n",
    "\n",
    "  Compare with the following models:\n",
    "  * Comet   : http://www.hakank.org/comet/ski_assignment.co\n",
    "  * MiniZinc: http://hakank.org/minizinc/ski_assignment.mzn\n",
    "  * ECLiPSe : http://www.hakank.org/eclipse/ski_assignment.ecl\n",
    "  * SICStus: http://hakank.org/sicstus/ski_assignment.pl\n",
    "  * Gecode: http://hakank.org/gecode/ski_assignment.cpp\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "\n",
    "def main():\n",
    "\n",
    "  # Create the solver.\n",
    "  solver = pywrapcp.Solver('Ski assignment')\n",
    "\n",
    "  #\n",
    "  # data\n",
    "  #\n",
    "  num_skis = 6\n",
    "  num_skiers = 5\n",
    "  ski_heights = [1, 2, 5, 7, 13, 21]\n",
    "  skier_heights = [3, 4, 7, 11, 18]\n",
    "\n",
    "  #\n",
    "  # variables\n",
    "  #\n",
    "\n",
    "  # which ski to choose for each skier\n",
    "  x = [solver.IntVar(0, num_skis - 1, 'x[%i]' % i) for i in range(num_skiers)]\n",
    "  z = solver.IntVar(0, sum(ski_heights), 'z')\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "  solver.Add(solver.AllDifferent(x))\n",
    "\n",
    "  z_tmp = [\n",
    "      abs(solver.Element(ski_heights, x[i]) - skier_heights[i])\n",
    "      for i in range(num_skiers)\n",
    "  ]\n",
    "  solver.Add(z == sum(z_tmp))\n",
    "\n",
    "  # objective\n",
    "  objective = solver.Minimize(z, 1)\n",
    "\n",
    "  #\n",
    "  # search and result\n",
    "  #\n",
    "  db = solver.Phase(x, solver.INT_VAR_DEFAULT, solver.INT_VALUE_DEFAULT)\n",
    "\n",
    "  solver.NewSearch(db, [objective])\n",
    "\n",
    "  num_solutions = 0\n",
    "  while solver.NextSolution():\n",
    "    num_solutions += 1\n",
    "    print('total differences:', z.Value())\n",
    "    for i in range(num_skiers):\n",
    "      x_val = x[i].Value()\n",
    "      ski_height = ski_heights[x[i].Value()]\n",
    "      diff = ski_height - skier_heights[i]\n",
    "      print('Skier %i: Ski %i with length %2i (diff: %2i)' %\\\n",
    "            (i, x_val, ski_height, diff))\n",
    "    print()\n",
    "\n",
    "  solver.EndSearch()\n",
    "\n",
    "  print()\n",
    "  print('num_solutions:', num_solutions)\n",
    "  print('failures:', solver.Failures())\n",
    "  print('branches:', solver.Branches())\n",
    "  print('WallTime:', solver.WallTime())\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
